package xyz.cerberu.cet.user.configuration;

import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.cerberu.cet.user.domain.MinIo;

/**
 * @author β世界
 * Created on 15:10
 * @Description MinIO的配置类
 * 当容器中存在MinIO容器，并且状态为开启及加载
 */
@Configuration
@ConditionalOnBean(MinIo.class)
@ConditionalOnProperty(name = {"minio.enable"},havingValue = "true")
public class MinIoConfig {

    private final MinIo minIo;

    public MinIoConfig(MinIo minIo) {
        this.minIo = minIo;
    }

    /**
     * @author β世界 on 13:14 2021/3/29
     * 创建MinIo连接客户端
     * @return io.minio.MinioClient
     **/
    @Bean
    public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
        return new MinioClient(minIo.getEndpoint(), minIo.getAccessKey(), minIo.getSecretKey());
    }

}
